//
//  RootViewController.m
//  AnimationTabBar
//
//  Created by I三生有幸I on 15/6/6.
//  Copyright (c) 2015年 盛辰. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation RootViewController
- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton *navTitleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    navTitleButton.frame = CGRectMake(0, 0, 100, 30);
    navTitleButton.backgroundColor = [UIColor redColor];
    [navTitleButton setTitle:@"求点" forState:UIControlStateNormal];
    [navTitleButton addTarget:self action:@selector(navTitleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = navTitleButton;
    
    navTitleButton.alpha = 0.3;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationRepeatCount:FLT_MAX];
    navTitleButton.alpha = 1.0;
    [UIView commitAnimations];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake((375 - 100) / 2, 0, 100, 0) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
}

- (void)navTitleButtonAction:(UIButton *)button
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"HiddenButton" object:nil];

    if (self.isShow == YES)
    {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.duration = 1;
        animation.toValue = [NSNumber numberWithFloat: M_PI * 5.0 ];
        [self.tableView.layer addAnimation:animation forKey:nil];
       
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationRepeatCount:1];
        self.tableView.frame = CGRectMake((375 - 100) / 2, 0, 100, 0);
        self.tableView.alpha = 0;

        [UIView commitAnimations];
        self.isShow = NO;
    }
    else
    {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.duration = 1;
        animation.toValue = [NSNumber numberWithFloat:M_PI * 5.0];
        [self.tableView.layer addAnimation:animation forKey:nil];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationRepeatCount:1];
        self.tableView.frame = CGRectMake((375 - 100) / 2, 0, 100, 90);
        self.tableView.alpha = 1;
        [UIView commitAnimations];
        self.isShow = YES;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndetifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndetifier];
    }
    if (indexPath.row == 0)
    {
        cell.textLabel.text = @"摇一摇";
    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = @"附近的人";
    }
    else
    {
        cell.textLabel.text = @"约饭";
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
